<template>
	<view class="container">
		<view class="provinces">
			<text class="name" v-for="(province, pIndex) in provinceList" :key="pIndex"
				:class="provinceIndex == pIndex ? 'current': ''" @click="changeProvince(pIndex)">{{ province.name }}</text>
		</view>
		<view class="cities">
			<text class="name" v-for="(city, cIndex) in cityList" :key="cIndex" @click="changeCity(city.name)">{{ city.name }}</text>
		</view>
	</view>
</template>

<script>
	import {
		getProvinces,
		getCities
	} from '@/api/common.js'
	export default {
		data() {
			return {
				provinceList: null,
				provinceIndex: 0,

				cityList: null
			}
		},
		onLoad() {
			this.listProvince();
		},
		methods: {
			// 省份列表
			listProvince() {
				const _that = this;
				getProvinces()
					.then((res) => {
						_that.provinceList = res.data;
						if (_that.provinceList && _that.provinceList.length > 0) {
							_that.listCity();
						}
					});
			},
			// 城市列表
			listCity() {
				const _that = this;
				const provinceName = _that.provinceList[_that.provinceIndex].name;
				getCities(provinceName)
					.then((res) => {
						_that.cityList = res.data;
					});
			},
			// 切换省份
			changeProvince(pIndex) {
				this.provinceIndex = pIndex;
				this.listCity();
			},
			// 切换城市
			changeCity(cityName) {
				uni.navigateTo({
					url: `/pages/metro/index?city=${encodeURIComponent(cityName)}`
				})
			}
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;

		.provinces {
			width: 40vw;
			height: 100vh;
			overflow: hidden;
			box-sizing: border-box;

			.name {
				width: 100%;
				height: 2.4rem;
				display: block;
				line-height: 2.4rem;
				padding: 0 3vw;
				border-right: 1px solid #dddddd;

				&.current {
					background-color: white;
					border-right-color: white;
				}
			}
		}

		.cities {
			width: 60vw;
			height: 100vh;
			overflow: hidden;
			background-color: white;

			.name {
				width: 100%;
				height: 2.4rem;
				display: block;
				line-height: 2.4rem;
				padding: 0 3vw;
			}
		}
	}
</style>
